package com.aloneness.dingtalk.learn.controller;

import com.aloneness.dingtalk.learn.domain.Express;
import com.aloneness.dingtalk.learn.domain.ExpressDetail;
import com.aloneness.dingtalk.learn.domain.ResponseBean;
import com.aloneness.dingtalk.learn.service.IExpressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/express")
public class ExpressController {

    @Autowired
    private IExpressService expressService;

    @GetMapping("/list")
    public ResponseBean listExpress(int pageNum, int pageSize, String keyword){
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("express_id desc");
        List<Express> expresses = expressService.selectExpressList(keyword);
        PageInfo<Express> pageInfo = new PageInfo<>(expresses);
        return ResponseBean.success(pageInfo.getList());
    }

    @GetMapping("/get")
    public ResponseBean listExpress(@RequestParam(value = "expressId") int expressId){
        Express express = expressService.selectExpressById(expressId);
        return ResponseBean.success(express);
    }

    @PostMapping("/update")
    public ResponseBean updateExpress(Express express){
        int result = expressService.updateExpress(express);
        return ResponseBean.success(result);
    }

    @PostMapping("/finish")
    public ResponseBean finishExpress(@RequestParam("expressId") String expressId){
        Express express = new Express();
        express.setExpressId(Integer.parseInt(expressId));
        int result = expressService.finishExpress(express);
        return ResponseBean.success(result);
    }

    @PostMapping("/cancel")
    public ResponseBean cancelExpress(@RequestParam("expressId") String expressId){
        Express express = new Express();
        express.setExpressId(Integer.parseInt(expressId));
        int result = expressService.cancelExpress(express);
        return ResponseBean.success(result);
    }

    @GetMapping("/publish/list")
    public ResponseBean listPublishExpress(String publishId){
        List<Express> expresses = expressService.selectPublishExpressList(publishId);
        return ResponseBean.success(expresses);
    }

    @GetMapping("/accept/list")
    public ResponseBean listAcceptExpress(String acceptId){
        List<Express> expresses = expressService.selectAcceptExpressList(acceptId);
        return ResponseBean.success(expresses);
    }

    @PostMapping("/add")
    public ResponseBean addExpress(@RequestParam("publishId") String publishId,
                                   @RequestParam("expressCompany") String expressCompany,
                                   @RequestParam("expressMessage") String expressMessage,
                                   @RequestParam("expressPhone") String expressPhone,
                                   @RequestParam("expressSite") String expressSite,
                                   @RequestParam("expressReward") String expressReward,
                                   @RequestParam("expressContact") String expressContact,
                                   @RequestParam(value = "expressNote", required = false) String expressNote){
        ExpressDetail expressDetail = new ExpressDetail();
        expressDetail.setExpressCompany(expressCompany);
        expressDetail.setExpressPublishTime(new Date());
        expressDetail.setExpressMessage(expressMessage);
        expressDetail.setExpressPhone(expressPhone);
        expressDetail.setExpressSite(expressSite);
        expressDetail.setExpressReward(new BigDecimal(expressReward));
        expressDetail.setExpressContact(expressContact);
        expressDetail.setExpressNote(expressNote);

        Express express = new Express();
        express.setExpressDetail(expressDetail);
        express.setPublishId(publishId);
        express.setOrderStatus(1001);
        return ResponseBean.success(expressService.insertExpress(express));
    }
}
